var nationValue = "";
var regionsValue = "";
var cityValue = "";

var nation = "";
var regions = "";
var city = "";
var telephone = "";
var gradSchool = "";
var intheclass = "";

var payType = "";
$(document).ready(function () {
	
	//alert($.md5("Hello"));
	
	$.ajax({
		type:'GET',
		cache:false,
		url:rootPath+"/getOrdersByStudentId",
		async:false,
		data:{
			StudentId:student_id
		},
		success:function(result)
		{
			payType = result.extend.code;
		}
	});
	
	$.ajax({
		type: 'GET',
		cache:false,
		url: rootPath + "/getStudentById",
		async: false,
		data: {
			StudentId: student_id
		},
		success: function (result)
		{
			var promiseType = result.extend.student.promiseType;
			if (promiseType == 1 && payType == 1)
				window.location.href = "affirmMess.html";
			
			var cardId = result.extend.student.cardId;
			var graduationType = result.extend.student.graduationType;
			var examineeType = result.extend.student.examineeType;
			
			document.getElementById("cardId").innerHTML = cardId;
			switch (graduationType)
			{
				case 1:
					document.getElementById("graduationType").innerHTML = '高中';
					break;
				case 2:
					document.getElementById("graduationType").innerHTML = '中等师范';
					break;
				case 3:
					document.getElementById("graduationType").innerHTML = '中等专业学校';
					break;
				case 4:
					document.getElementById("graduationType").innerHTML = '职业高中学校';
					break;
				case 5:
					document.getElementById("graduationType").innerHTML = '技工学校';
					break;
			}
			switch (examineeType)
			{
				case 1:
					document.getElementById("examineeType").innerHTML = '城市应届';
					break;
				case 2:
					document.getElementById("examineeType").innerHTML = '农村应届';
					break;
				case 3:
					document.getElementById("examineeType").innerHTML = '城市往届';
					break;
				case 4:
					document.getElementById("examineeType").innerHTML = '农村往届';
					break;
			}
			
			var cardId = result.extend.student.cardId;
			var graduationType = result.extend.student.graduationType;
			var examineeType = result.extend.student.examineeType;
			
			document.getElementById("cardId").innerHTML = cardId;
			switch (graduationType)
			{
				case 1:
					document.getElementById("graduationType").innerHTML = '高中';
					break;
				case 2:
					document.getElementById("graduationType").innerHTML = '中等师范';
					break;
				case 3:
					document.getElementById("graduationType").innerHTML = '中等专业学校';
					break;
				case 4:
					document.getElementById("graduationType").innerHTML = '职业高中学校';
					break;
				case 5:
					document.getElementById("graduationType").innerHTML = '技工学校';
					break;
			}
			switch (examineeType)
			{
				case 1:
					document.getElementById("examineeType").innerHTML = '城市应届';
					break;
				case 2:
					document.getElementById("examineeType").innerHTML = '农村应届';
					break;
				case 3:
					document.getElementById("examineeType").innerHTML = '城市往届';
					break;
				case 4:
					document.getElementById("examineeType").innerHTML = '农村往届';
					break;
			}

			var eamineeId = result.extend.student.eamineeId;
			var name = result.extend.student.name;
			var birthday = result.extend.student.birthday;
			var gender = result.extend.student.gender;
			
			document.getElementById("eamineeId").value = eamineeId;
			document.getElementById("name").value = name;
			document.getElementById("birthday").value = birthday;
			var genderRadio = document.getElementsByName("genderRadio");
			for (var i = 0; i < genderRadio.length; i++)
			{
				if (genderRadio[i].value == gender)
					genderRadio[i].checked = true;
			}
			
			nation = result.extend.student.nation; //需要填写的值
			regions = result.extend.student.regions;
			city = result.extend.student.city;
			telephone = result.extend.student.telephone;
			gradSchool = result.extend.student.gradSchool;
			intheclass = result.extend.student.intheclass;

			nationValue = nation;
			regionsValue = regions;
			cityValue = city;
			
			if(nationValue != null && nationValue != "null" && nationValue != "")
			{
				document.getElementById("telephone").value = telephone;
				document.getElementById("gradSchool").value = gradSchool;
				document.getElementById("intheclass").value = intheclass;
			}
		}
	});

	$.ajax({
		type: 'GET',
		cache:false,
		url: rootPath + "/nation",
		async: true,
		success: function (result)
		{
			var nationSelect = "<option value = 'null'>----请选择----</option>";
			var nation = result.extend.nation;
			var nationValueId = '';
			for (var i = 0; i < nation.length; i++)
			{
				if(nation[i].nation === nationValue){
					nationValueId = nation[i].id;
				}
				nationSelect += "<option value =" + nation[i].id + ">" + nation[i].nation + "</option>";
			}
			$('#nationSelect').html(nationSelect);
			if(nationValue){
				$("#nationSelect").val(nationValueId);
			}
//			var nationSelects = document.getElementById("nationSelect");
//			nationSelects.innerHTML = nationSelect;
//			for (var i = 0; i < nationSelects.length; i++)
//			{
//				if (nationSelects[i].text == nationValue)
//					nationSelects[i].selected = true;
//			}
		}
	});

	$.ajax({
		type: 'GET',
		cache:false,
		url: rootPath + "/getpid",
		async: true,
		data: {
			pid: '1000'
		},
		success: function (result)
		{
			var citySelect = "<option value = 'null'>----请选择----</option>";
			var city = result.extend.region;
			var cityValueId = '';
			for (var i = 0; i < city.length; i++)
			{
				citySelect += "<option value =" + city[i].id + ">" + city[i].region + "</option>";
				if(city[i].region === regionsValue){
					cityValueId = city[i].id;
				}
			}
			//var citySelects = document.getElementById("citySelect");
			$("#citySelect").html(citySelect);
			if(regionsValue){
				$("#citySelect").val(cityValueId);
				changeCitySelect();
			}
//			citySelects.innerHTML = citySelect;
//			for (var i = 0; i < citySelects.length; i++)
//			{
//				if (citySelects[i].text == regionsValue)
//				{
//					citySelects[i].selected = true;
//					changeCitySelect();
//				}
//			}
		}
	});
});

function changeCitySelect()
{
	var citySelect = document.getElementById("citySelect");
	var id;
	for (var i = 0; i < citySelect.length; i++)
	{
		if (citySelect[i].selected)
			id = citySelect[i].value;
	}

	$.ajax({
		type: 'GET',
		url: rootPath + "/getpid",
		async: true,
		data: {
			pid: id
		},
		success: function (result)
		{
			var areaSelect = "<option value = 'null'>----请选择----</option>";
			var area = result.extend.region;
			var areaValueId = '';
			for (var i = 0; i < area.length; i++)
			{
				areaSelect += "<option value =" + area[i].id + ">" + area[i].region + "</option>";
				if(area[i].region === cityValue){
					areaValueId = area[i].id;
				}
			}
			$("#areaSelect").html(areaSelect);
			if(cityValue){
				$("#areaSelect").val(areaValueId);
			}
//			var areaSelects = document.getElementById("areaSelect");
//			areaSelects.innerHTML = areaSelect;
//			for (var i = 0; i < areaSelects.length; i++)
//			{
//				if (areaSelects[i].text == cityValue)
//					areaSelects[i].selected = true;
//			}
		}
	});
}

function ok()
{
	var nationSelect = document.getElementById("nationSelect");
	var citySelect = document.getElementById("citySelect");
	var areaSelect = document.getElementById("areaSelect");

	for (var i = 0; i < nationSelect.length; i++)
	{
		if (nationSelect[i].selected)
			nation = nationSelect[i].text;
	}
	var provinces = "吉林省";
	for (var i = 0; i < citySelect.length; i++)
	{
		if (citySelect[i].selected)
			regions = citySelect[i].text;
	}
	for (var i = 0; i < areaSelect.length; i++)
	{
		if (areaSelect[i].selected)
			city = areaSelect[i].text;
	}
	telephone = document.getElementById("telephone").value;
	gradSchool = document.getElementById("gradSchool").value;
	intheclass = document.getElementById("intheclass").value;

	if (nation != "----请选择----")
		if (regions != "----请选择----")
			if (city != "----请选择----")
				if ((/(^(1)\d{10}$)/.test(telephone)))
					if (gradSchool != "" && gradSchool.length<=20)
						if (intheclass != "" && intheclass.length<=10)
							passByValue(nation, provinces, regions, city, telephone, gradSchool, intheclass);
						else
							alert("请输入所在班级并且字数在10以内");
					else
						alert("请输入毕业学校并且字数在20以内");
				else
					alert("联系电话错误");
			else
				alert("请选择城市");
		else
			alert("请选择地区");
	else
		alert("请选择民族");
}

function passByValue(nation, provinces, regions, city, telephone, gradSchool, intheclass)
{
	$.ajax({
		type: 'GET',
		url: rootPath + "/updateStudent",
		async: true,
		data: {
			id: student_id,
			nation: nation,
			languages: '英语',
			provinces: provinces,
			regions: regions,
			city: city,
			telephone: telephone,
			gradSchool: gradSchool,
			intheclass: intheclass
		},
		success: function (result)
		{
			if (result.code == 100)
			{
				window.location.href = "volunteerMessFill.html";
			}
			else
			{
				alert(result.extend.code);
			}
		}
	});
}

function resetValue()
{
	var nationSelects = document.getElementById("nationSelect");
	var citySelects = document.getElementById("citySelect");
	var areaSelects = document.getElementById("areaSelect");

	nationSelects[0].selected = true;
	citySelects[0].selected = true;
	areaSelects.innerHTML = "";

	document.getElementById("telephone").value = "";
	document.getElementById("gradSchool").value = "";
	document.getElementById("intheclass").value = "";
}

function toVolunteerMessFill()
{
	if (nation)
	{
		if (regions)
			if (city)
				if (telephone)
					if (gradSchool)
						if (intheclass)
							window.location.href = "volunteerMessFill.html";
	}
	else
	{
		alert("请点击下一步");
	}
}

function toSiteMessFill()
{
	if (nation)
	{
		if (regions)
			if (city)
				if (telephone)
					if (gradSchool)
						if (intheclass)
							window.location.href = "siteMessFill.html";
	}
	else
	{
		alert("请点击下一步");
	}
}
